<?php require("../../php/config.php") ; ?>
var pads;
var padsIndex;
var searchText;
var padsScroller;
var currentViewKey = "";
var currentCallback = false;
var viewKeys = new Object();
var fadeOutNotification = null;

function Init() {
	ResetView();
	setInterval(ViewKeyWatcher, 50);
	InitGoogleTracker();
	<?php foreach($views as $key=>$value) { ?>
	RegisterViewKey("<?php echo $key; ?>");
	<?php } ?>
}

function InitGoogleTracker()
{
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-16514018-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
}

function RegisterPad(GUID)
{
	var pad = new Object();
	pad.GUID = GUID;
	pad.ContainerElement = document.getElementById("Pad_" + GUID);
	pad.TabsElement = document.getElementById("Pad_Tabs_" + GUID);
	pad.TabsTitleElement = document.getElementById("Pad_Tabs_Title_" + GUID);
	pad.Title = pad.TabsTitleElement.innerHTML;
	pad.BaseElement = document.getElementById("Pad_Base_" + GUID);
	pad.Main = new Object();
	pad.Main.TabElement = document.getElementById("Pad_Main_Tab_" + GUID);
	pad.Main.ContentElement = document.getElementById("Pad_Main_Content_" + GUID);
	pad.Post = new Object();
	pad.Post.TabElement = document.getElementById("Pad_Post_Tab_" + GUID);
	pad.Post.ContentElement = document.getElementById("Pad_Post_Content_" + GUID);
	pad.Settings = new Object();
	pad.Settings.TabElement = document.getElementById("Pad_Settings_Tab_" + GUID);
	pad.Settings.ContentElement = document.getElementById("Pad_Settings_Content_" + GUID);
	pad.CurrentView = 0;
	pad.ShowMainTab = function () {
		pad.CurrentView = 0;
		pad.TabsElement.style.backgroundPosition = "0px -55px";
		pad.TabsTitleElement.style.color = "#FFFFFF";
		pad.Main.TabElement.style.visibility = "hidden";
		pad.Post.TabElement.style.visibility = "visible";
		pad.Settings.TabElement.style.visibility = "visible";
		pad.BaseElement.style.borderColor = "#66CC00";
		pad.BaseElement.style.backgroundColor = "#EEFFEE";
		pad.Main.ContentElement.style.display = "block";
		pad.Post.ContentElement.style.display = "none";
		pad.Settings.ContentElement.style.display = "none";
	};
	pad.ShowPostTab = function () {
		pad.CurrentView = 1;
		pad.TabsElement.style.backgroundPosition = "0px -77px";
		pad.TabsTitleElement.style.color = "#999999";
		pad.BaseElement.style.borderColor = "#006699";
		pad.BaseElement.style.backgroundColor = "#EEEEFF";
		pad.Main.ContentElement.style.display = "none";
		pad.Post.ContentElement.style.display = "block";
		pad.Settings.ContentElement.style.display = "none";
		pad.Main.TabElement.style.visibility = "visible";
		pad.Post.TabElement.style.visibility = "hidden";
		pad.Settings.TabElement.style.visibility = "visible";
	};
	pad.ShowSettingsTab = function () {
		pad.CurrentView = 2;
		pad.TabsElement.style.backgroundPosition = "0px -98px";
		pad.TabsTitleElement.style.color = "#999999";
		pad.BaseElement.style.borderColor = "#666666";
		pad.BaseElement.style.backgroundColor = "#EEEEEE";
		pad.Main.ContentElement.style.display = "none";
		pad.Post.ContentElement.style.display = "none";
		pad.Settings.ContentElement.style.display = "block";
		pad.Main.TabElement.style.visibility = "visible";
		pad.Post.TabElement.style.visibility = "visible";
		pad.Settings.TabElement.style.visibility = "hidden";
	};
	pad.Highlight = function() {
		//Highlight Pad
	}
	pad.Main.TabElement.addEvent('click', pad.ShowMainTab);
	pad.Post.TabElement.addEvent('click', pad.ShowPostTab);
	pad.Settings.TabElement.addEvent('click', pad.ShowSettingsTab);
	pad.ShowMainTab();
	pads[pad.Title] = pad;
	padsIndex.push(pad.Title);
}
function RegisterPads(pads)
{
	for (var i = 0; i < pads.length; i++) {
		RegisterPad(pads[i]);
	}
}
function InitSearchForm() {
	searchText = $('SearchText');
	searchText.HelpText = "Search...";
	searchText.GotFocus = function () {
		searchText.isFocused = true;
		searchText.style.fontStyle = "Normal";
		searchText.style.color = "#000000";
		if (searchText.value == searchText.HelpText) {
			searchText.value = "";
		}
	};
	searchText.LostFocus = function () {
		searchText.isFocused = false;
		if (searchText.value == "") {
			searchText.style.fontStyle = "Italic";
			searchText.style.color = "#999999";
			searchText.value = searchText.HelpText;
		}
	};
	searchText.TextChange = function() {
		if (searchText.isFocused && searchText.value != "" && searchText.value != searchText.HelpText) {
			for(var i=0;i<padsIndex.length;i++) {
				if (StartsWith(padsIndex[i], searchText.value, true)) {
					pads[padsIndex[i]].Highlight();
					padsScroller.toElement(pads[padsIndex[i]].ContainerElement);
					return;
				}
			}
			padsScroller.start(0,0);
		} else if (searchText.isFocused && searchText.value == "") {
			padsScroller.start(0,0);
		}
	};
	searchText.addEvent('focus', searchText.GotFocus);
	searchText.addEvent('blur', searchText.LostFocus);
	searchText.addEvent('keyup', searchText.TextChange);
	try { searchText.addEventListener("change", searchText.TextChange, false); } catch (e) { }
	try { searchText.attachEvent("onchange", searchText.TextChange); } catch (e) { }
	searchText.LostFocus();
}
function StartsWith(baseString, stringMatch, caseSensitive)
{
	if (caseSensitive) {
		baseString = baseString.toLowerCase();
		stringMatch = stringMatch.toLowerCase();
	}
	return (baseString.match("^"+stringMatch)==stringMatch)
};
function InitRecentNews() {
	$('RecentNewsOverlay').style.display = 'none';
	$('RecentNewsOverlay').Showing = false;
	$('RecentNewsOverlay').addEvent('click', function() { if ($('RecentNewsOverlay').Showing) { ToggleNewsFeed()} } );
	$('RecentNewsContainer').addEvent('click', function(event) { event.stop(); } );
}
function ToggleNewsFeed() {
	$('RecentNewsOverlay').style.display = $('RecentNewsOverlay').Showing? 'none' : 'block';
	$('RecentNewsOverlay').Showing = !$('RecentNewsOverlay').Showing;
}
function InitPadBoard() {
	pads = new Object();
	padsIndex = new Array();
	RegisterPads(arguments);
	InitSearchForm();
	InitRecentNews();
	padsScroller = new Fx.Scroll($('PadsContainer'), { wait: false, duration:750, transition: Fx.Transitions.Quad.easeInOut });
}

function ResetView() {
	$('MainContainer').innerHTML = '<center><img src="images/loader.gif" border="0" alt="Loading..." style="margin:250px 0px;"/></center>';
}

function RenderView(viewKey) {
	try {
		if (viewKeys[viewKey]) {
			ResetView();
			var req = new Request.HTML({
			  method: 'get', 
			  update: $('MainContainer'),
			  url: '<?php echo $baseUrl; ?>'+viewKey,
			  onComplete: function() { if (currentCallback) { setTimeout(currentCallback,1); currentCallback=false; } },
			  }).send();
		}
	} catch (e) {
	
	}
}

function RegisterViewKey(viewKey) {
	viewKeys[viewKey.toLowerCase()] = true;
}

function ViewKeyWatcher() {	
	try {
		var location = document.location + "";
		var anchor = location.indexOf("#")>0? location.substring(location.indexOf("#")+1).toLowerCase() : "";
		var currentViewKeyLower = currentViewKey.toLowerCase();
		if (anchor!="" && currentViewKeyLower!=anchor && viewKeys[anchor]) {
			RenderView(anchor);
			currentViewKey = anchor;
		} else if (anchor=="") {
			SetViewKey("Main");
		}
	} catch (e) {
		SetViewKey("Main");
	}
}

function UpdateViewKey(viewKey, title) {
	try {
		var location = document.location + "";
		var anchor = location.indexOf("#")>0? location.substring(location.indexOf("#")+1).toLowerCase() : "";
		var viewKeyLower = viewKey.toLowerCase();
		var currentViewKeyLower = currentViewKey.toLowerCase();
		if (currentViewKeyLower!=viewKeyLower && anchor!=viewKeyLower && viewKeys[viewKeyLower]) {
			currentViewKey = viewKey; //update the current key so as to not trigger the watcher
			document.location = "<?php echo $baseUrl; ?>#" + viewKey;
		}
		document.title = title;
	} catch (e) {
	
	}
}

function RefreshView() {
	RenderView(currentViewKey);
}

function SetViewKey(viewKey, callback) {
	try {
		if (viewKey.toLowerCase()==currentViewKey.toLowerCase()) {
			RefreshView();
		} else {
			document.location = "<?php echo $baseUrl; ?>#"+viewKey;
		}
		currentCallback = (typeof(callback)!=undefined)? callback : false;
	} catch (e) { }
}

function RegisterForm(formId) {
	try {
		$(formId).addEvent('submit', function(e) {
			e.stop();
			this.set('send', { evalScripts:true, onComplete: function(response) { $('MainContainer').set('html', response); } });
			this.send();
			ResetView();
		});
	} catch (e) {
	
	}
}

function ShowAuthHeader() {
	$('HeaderReqAuth').style.display = "block";
}
function HideAuthHeader() {
	$('HeaderReqAuth').style.display = "none";
}

function Notify(notification) {
	$('Notifications').setStyle('opacity','0');
	$('Notifications').innerHTML = notification;
	$('Notifications').fade('in');
	if (fadeOutNotification!=null) {
		clearTimeout(fadeOutNotification);
		fadeOutNotification = null;
	}
	var fadeOut = function() {
		$('Notifications').fade('out');
		fadeOutNotification = null;
	}
	fadeOutNotification = setTimeout(fadeOut, 10000);
}

function PopulateField(id, value) {
	try {
		if (typeof(value)!="undefined" && value!=null && value!="") {
			var element = $(id);
			var type = element.tagName.toLowerCase();
			if (type=="input") {
				element.value = value;
			} else if (type=="select") {
				element.value = value;
			}
		}
	} catch (e) {
	
	}
}

window.addEvent('domready', function() { Init(); })